package com.dz.common.web;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {

	private final int SUCCESS_CODE = 200;

	public Object beforeBodyWrite(Object data, MethodParameter methodParameter, MediaType arg2,
			Class<? extends HttpMessageConverter<?>> arg3, ServerHttpRequest arg4, ServerHttpResponse arg5) {
		@SuppressWarnings("rawtypes")
		Class declarClass = methodParameter.getDeclaringClass();
		String className = declarClass.getName();
		if (className.startsWith("com.dz")) {
			if (data != null) {
				return new ResultVO(SUCCESS_CODE, data, null);
			} else {
				return new ResultVO(SUCCESS_CODE, null, null);
			}
		} else {
			return data;
		}
	}

	@Override
	public boolean supports(MethodParameter arg0, Class<? extends HttpMessageConverter<?>> arg1) {
		return true;
	}
}
